package com.liz.tectSelect.nettySpringbootRestfulApi.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

//    @Resource
//    private DiscardServerHandler discardServerHandler;

    @Resource
    NettyHttpServerHandler nettyHttpServerHandler;

//    DefaultHttpRequest接收数据时，分开接收的httpRequest与HttpContent
//    FullHttpRequest，接收数据时，协议头和Form数据是在一起的，不用分开读，


    public void initChannel(SocketChannel socketChannel) throws Exception {
//        socketChannel.pipeline().addLast("decoder",stringDecoder );


        // 请求解码器
        socketChannel.pipeline().addLast("http-decoder", new HttpRequestDecoder());
        // 将HTTP消息的多个部分合成一条完整的HTTP消息
        socketChannel.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65535));
        // 响应转码器
        socketChannel.pipeline().addLast("http-encoder", new HttpResponseEncoder());
        // 解决大码流的问题，ChunkedWriteHandler：向客户端发送HTML5文件
        socketChannel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
        // 自定义处理handler
        socketChannel.pipeline().addLast("http-server", nettyHttpServerHandler);


//        socketChannel.pipeline().addLast("encoder",stringEncoder) ;
    }
}
